﻿using System;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using FlitBit.Core;

namespace FlitBit.IoC.Mvc.WebApi
{
    /// <summary>
    /// Registration in Global.asax -> GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new IoCEnabledWebApiControllerActivator());
    /// </summary>
    public class IoCEnabledWebApiControllerActivator : IHttpControllerActivator
    {
        public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
        {
            var currentFactory = FactoryProvider.Factory;
            if (currentFactory.CanConstruct(controllerType))
                return currentFactory.CreateInstance(controllerType) as IHttpController;
            return default(IHttpController);
        }
    }
}
